package com.xsz.controller;

import com.xsz.entity.Administrator;
import com.xsz.entity.User;
import com.xsz.service.UserService;
import com.xsz.util.MD5Utils;
import com.xsz.util.ResultVOUtil;
import com.xsz.vo.ResultVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.subject.Subject;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Map;

@RestController
@RequestMapping("user")
@Api(value = "用户控制类")
public class UserController {
    @Resource
    UserService userService;

    @ApiOperation(value = "注册API")
    @PostMapping("/register")
    public ResultVO add(@RequestBody User user) {
        if(userService.getByName(user.getName())!=null){
            return ResultVOUtil.error(500,"用户名已存在");
        }else{
            String password = MD5Utils.encrypt(user.getName(), user.getPwd());
            user.setPwd(password);
            return ResultVOUtil.success(userService.add(user));
        }
    }

    @ApiOperation(value = "登录API")
    @PostMapping("/login")
    public ResultVO selectByNamePwd(@RequestBody  User user, HttpServletRequest request) {
        String password = user.getPwd();
        String username = user.getName();
        password = MD5Utils.encrypt(username, password);
        try{
            User loginuser=userService.login(username,password);
            HttpSession session=request.getSession();
            session.setAttribute("loginuser",loginuser);
            return ResultVOUtil.success(loginuser);
        }catch (UnknownAccountException e) {
            return ResultVOUtil.error(500,"账号不存在");
        } catch (IncorrectCredentialsException e) {
            return ResultVOUtil.error(500,"密码错误");
        } catch (LockedAccountException e) {
            return ResultVOUtil.error(500,"账号被锁定");
        } catch (AuthenticationException e) {
            return ResultVOUtil.error(500,"登录失败");
        }

    }

    /**查询当前用户信息**/
    @ApiOperation(value = "用户信息")
    @GetMapping("/userinfo")
    public ResultVO showUserById(HttpServletRequest request){
        HttpSession session=request.getSession();
        User user =(User) session.getAttribute("loginuser");
        return ResultVOUtil.success(userService.selectById(user.getId()));
    }

    /**修改当前用户信息**/
    @ApiOperation(value = "修改用户信息")
    @PostMapping("/infoEdit")
    public ResultVO UserInfoEdit(@RequestBody  User user, HttpServletRequest request){
        HttpSession session=request.getSession();
        User loginuser =(User) session.getAttribute("loginuser");
        user.setId(loginuser.getId());
        user.setPwd(loginuser.getPwd());
        return ResultVOUtil.success(userService.update(user));
    }

    /**根据当前用户密码查询**/
    @ApiOperation(value = "密码查询")
    @PostMapping("/showUserByPwd")
    public ResultVO showUserByPwd(HttpServletRequest req,@RequestBody User user){
        User loginuser=(User)req.getSession().getAttribute("loginuser");
        user.setId(loginuser.getId());
        user.setName(loginuser.getName());
        return ResultVOUtil.success(userService.selectByIdAndPwd(user));
    }

    /**用户密码修改**/
    @ApiOperation(value = "密码修改")
    @PostMapping("/pwdEdit")
    public ResultVO pwdEdit(HttpServletRequest req,@RequestBody User user){
        User loginuser=(User)req.getSession().getAttribute("loginuser");
        user.setId(loginuser.getId());
        user.setName(loginuser.getName());
        userService.updatePwdById(user);
        return ResultVOUtil.success();
    }



}
